//
//  NoticeDetailViewController.m
//  FingerUniversity
//
//  Created by vantle on 16/3/16.
//  Copyright © 2016年 vantle. All rights reserved.
//

#import "NoticeDetailViewController.h"
#import "UIColor+hexColor.h"
#import "AFNetworking.h"
#import "SXMarquee.h"
#import "MTLJSONAdapter.h"
#import "NoticeModel.h"
#import "MobClick.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
@interface NoticeDetailViewController ()

@end
@implementation NoticeDetailViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [MobClick beginLogPageView:@"NoticeDetailViewController"];//("PageOne"为页面名称，可自定义)
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"NoticeDetailViewController"];
}
-(void)viewDidLoad
{
    self.view.backgroundColor=[UIColor whiteColor];
    UIImageView*bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    bg.image=[UIImage imageNamed:@"bg"];
    [self.view addSubview:bg];
    
    UIImageView*barImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, 44)];
    barImage.userInteractionEnabled=YES;
    barImage.backgroundColor=[UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:1];
    [self.view addSubview:barImage];
    
    UIButton*backButton=[UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame=CGRectMake(0, 0, 100, 44);
    [backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [barImage addSubview:backButton];
    
    UIImageView*backImage=[[UIImageView alloc]initWithFrame:CGRectMake(5, 12, 5, 20)];
    backImage.image=[UIImage imageNamed:@"back_arrowleft"];
    [barImage addSubview:backImage];
    
    UIImageView*logoImage=[[UIImageView alloc]initWithFrame:CGRectMake(backImage.frame.origin.x+backImage.frame.size.width+3, 6, 32, 32)];
    logoImage.layer.cornerRadius=3;
    logoImage.clipsToBounds=YES;
    logoImage.image=[UIImage imageNamed:@"app-logo-80"];
    [barImage addSubview:logoImage];
    
    UILabel*noticeLabel=[[UILabel alloc]initWithFrame:CGRectMake(logoImage.frame.origin.x+logoImage.frame.size.width+5, 10, 100, 25)];
    noticeLabel.text=@"公告详情";
    [barImage addSubview:noticeLabel];
    
    _bg=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64)];
    _bg.userInteractionEnabled=YES;
    _bg.image=[UIImage imageNamed:@"bg"];
    [self.view addSubview:_bg];
    
    [self getNoticeInfo];
    
}
-(void)setContent
{
    
    UIImageView*whiteBG=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, ScreenWidth-20, ScreenHeight-84)];
    whiteBG.backgroundColor=[UIColor whiteColor];
    [_bg addSubview:whiteBG];
    
    UILabel*gongGaoLabel=[[UILabel alloc]init];
    gongGaoLabel.center=CGPointMake(whiteBG.center.x, 40);
    gongGaoLabel.bounds=CGRectMake(0, 0, 100, 40);
    gongGaoLabel.text=@"公告";
    gongGaoLabel.textAlignment=1;
    gongGaoLabel.textColor=[UIColor hexFloatColor:@"eb603a"];
    gongGaoLabel.font=[UIFont systemFontOfSize:25];
    [_bg addSubview:gongGaoLabel];
    
    UIImageView*line=[[UIImageView alloc]init];
    line.center=CGPointMake(whiteBG.center.x, gongGaoLabel.center.y+20);
    line.bounds=CGRectMake(0, 0, ScreenWidth-30, 1);
    line.backgroundColor=[UIColor hexFloatColor:@"d86947"];
    [_bg addSubview:line];
    
    CGSize title = [_title sizeWithFont:[UIFont systemFontOfSize:21] constrainedToSize:CGSizeMake(MAXFLOAT, 25) lineBreakMode:UILineBreakModeWordWrap];
    if (title.width>whiteBG.frame.size.width-20) {
        SXMarquee *mar2 = [[SXMarquee alloc]initWithFrame:CGRectMake(10, line.center.y+15, _bg.frame.size.width-20, 25) speed:4 Msg:_title];
        [_bg addSubview:mar2];
            [mar2 changeMarqueeLabelFont:[UIFont systemFontOfSize:21]];
        [mar2 start];

    }
    else
    {
        _titleLabel=[[UILabel alloc]init];
        _titleLabel.center=CGPointMake(whiteBG.center.x, line.center.y+15);
        _titleLabel.bounds=CGRectMake(0, 0, _bg.frame.size.width, 25);
        _titleLabel.font=[UIFont systemFontOfSize:21];
        _titleLabel.text=_title;
        _titleLabel.textAlignment=1;
        [_bg addSubview:_titleLabel];

    }
    
    
//    CGSize title = [_titleLabel.text sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(MAXFLOAT, 25) lineBreakMode:UILineBreakModeWordWrap];
//    if (title.width>whiteBG.frame.size.width-20) {
//        [self timer];
//    }
    
    UILabel*pulishPeopleLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, line.frame.origin.y+line.frame.size.height+40, 60, 20)];
    pulishPeopleLabel.text=@"发布人:";
    pulishPeopleLabel.font=[UIFont systemFontOfSize:15];
    pulishPeopleLabel.textColor=[UIColor hexFloatColor:@"7e8399"];
    [_bg addSubview:pulishPeopleLabel];
    
    UILabel*peopleLabel=[[UILabel alloc]initWithFrame:CGRectMake(70, line.frame.origin.y+line.frame.size.height+40, 60, 20)];
    peopleLabel.text=_creator;
    peopleLabel.font=[UIFont systemFontOfSize:15];
    peopleLabel.textColor=[UIColor hexFloatColor:@"7e8399"];
    [_bg addSubview:peopleLabel];
    
    UILabel*pulishTimeLabel=[[UILabel alloc]initWithFrame:CGRectMake(20, pulishPeopleLabel.frame.origin.y+pulishPeopleLabel.frame.size.height+5, 100, 20)];
    pulishTimeLabel.text=@"发布时间:";
    pulishTimeLabel.font=[UIFont systemFontOfSize:15];
    pulishTimeLabel.textColor=[UIColor hexFloatColor:@"7e8399"];
    [_bg addSubview:pulishTimeLabel];
    
    UILabel*timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(100, pulishPeopleLabel.frame.origin.y+pulishPeopleLabel.frame.size.height+5, 200, 20)];
    timeLabel.text=_createTime;
    timeLabel.font=[UIFont systemFontOfSize:15];
    timeLabel.textColor=[UIColor hexFloatColor:@"7e8399"];
    [_bg addSubview:timeLabel];
    
    UIImageView*redKuangImage=[[UIImageView alloc]init];
    redKuangImage.image=[UIImage imageNamed:@"juxing"];
    redKuangImage.frame=CGRectMake(20, pulishTimeLabel.frame.origin.y+pulishTimeLabel.frame.size.height+10, whiteBG.frame.size.width-20, whiteBG.frame.size.height-pulishTimeLabel.frame.origin.y-pulishTimeLabel.frame.size.height-10);
    [_bg addSubview:redKuangImage];
    
//    UIImageView*logoBottom=[[UIImageView alloc]init];
//    logoBottom.center=CGPointMake(whiteBG.center.x, ScreenHeight-90);
//    logoBottom.bounds=CGRectMake(0, 0, 50, 50);
//    logoBottom.image=[UIImage imageNamed:@"login_logoicon"];
//    [_bg addSubview:logoBottom];
    
    NSString*str=_content;
    
    CGSize detailSize =[str sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(redKuangImage.frame.size.width-40, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    
    UIScrollView*scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(redKuangImage.frame.origin.x, redKuangImage.frame.origin.y+5, redKuangImage.frame.size.width, redKuangImage.frame.size.height-10)];
    scrollView.contentSize=CGSizeMake(redKuangImage.frame.size.width, detailSize.height+80);
    [_bg addSubview:scrollView];
    
    UILabel*textTitleLabel=[[UILabel alloc]init];
    textTitleLabel.center=CGPointMake(ScreenWidth/2-10,30);
    textTitleLabel.text=_title;
    textTitleLabel.textAlignment=1;
    textTitleLabel.numberOfLines=0;
    textTitleLabel.bounds=CGRectMake(0, 0, redKuangImage.frame.size.width-40,80);
    [scrollView addSubview:textTitleLabel];
    
    UILabel*contentLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, textTitleLabel.frame.origin.y+textTitleLabel.frame.size.height, redKuangImage.frame.size.width-20, detailSize.height)];
    contentLabel.alpha=0.9;
    contentLabel.text=str;
    contentLabel.numberOfLines=0;
    [scrollView addSubview:contentLabel];
    
    
}

-(void)getNoticeInfo
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString*userId=[userDefaults objectForKey:@"userId"];
    NSLog(@"======%@",userId);
    NSString*flag=[userDefaults objectForKey:@"flag"];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    manager.responseSerializer.acceptableContentTypes =[manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
    
    NSDictionary*params=@{@"userId":userId,@"flag":flag,@"noticeId":self.noticeIdQu};
    
    [manager POST:@"http://120.76.79.85:9080/mzdx/service/noticeDetail.action" parameters:params success: ^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData*responseData=responseObject;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData
                                                            options:NSJSONReadingMutableContainers
                                                              error:nil];
        _createTime=[dic objectForKey:@"createTime"];
        _creator=[dic objectForKey:@"creator"];
        NSString *string= [[dic objectForKey:@"content"] stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
        _title=[dic objectForKey:@"title"];
        _content=[self flattenHTML:string];
        if (dic!=nil) {
            [self setContent];
        }
        
    } failure: ^(AFHTTPRequestOperation * operation, NSError *error) {
        NSLog(@"%@", error);
    }];
    
}
- (NSString *)flattenHTML:(NSString *)html {
    
    NSScanner *theScanner;
    NSString *text = nil;
    
    theScanner = [NSScanner scannerWithString:html];
    
    while ([theScanner isAtEnd] == NO) {
        
        [theScanner scanUpToString:@"<" intoString:NULL] ;
        
        [theScanner scanUpToString:@">" intoString:&text] ;
        
        html = [html stringByReplacingOccurrencesOfString:
                [NSString stringWithFormat:@"%@>", text]
                                               withString:@""];
    }
    
    NSLog(@"-----===%@",html);
    return html;
}
-(void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
